package com.mentaleye.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.DictionaryBean;
import com.mentaleye.bean.OaClassBean;
import com.mentaleye.bean.OaClassRelationBean;
import com.mentaleye.bean.TeacherBean;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.biz.OaClassBiz;
import com.mentaleye.biz.OaClassRelationBiz;
import com.mentaleye.biz.TeacherBiz;
import com.mentaleye.dal.model.OaClassExample;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.MultipleTansferUtil;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

	@Autowired
	private TeacherBiz teacherBiz;
	@Autowired
	private OaClassRelationBiz oaClassRelationBiz;
	@Autowired
	private OaClassBiz oaClassBiz;
	@Autowired
	private DictionaryBiz dictionaryBiz;
	
	//获得教师列表视图
	@RequestMapping("/teacherList")
	public ModelAndView list() {
		ModelAndView view = new ModelAndView("ea/teacher/teacherList");
		Map<Integer, String> teacherSexMap = dictionaryBiz
				.queryDictionaryByParent(DictionaryEnums.SEX.getCode());
		Map<Integer, String> teacherPositionMap = dictionaryBiz
				.queryDictionaryByParent(DictionaryEnums.JOB.getCode());
		Map<Integer, String> teacherStatusMap = dictionaryBiz
				.queryDictionaryByParent(DictionaryEnums.TEACHER_STATUS.getCode());
		Map<Integer, String> teacherEducationMap = dictionaryBiz
				.queryDictionaryByParent(DictionaryEnums.EDUCATION.getCode());
		view.addObject("teacherSexMap", teacherSexMap);
		view.addObject("teacherPositionMap", teacherPositionMap);
		view.addObject("teacherStatusMap", teacherStatusMap);
		view.addObject("teacherEducationMap", teacherEducationMap);
		view.addObject("teacherSex", JSONObject.toJSONString(teacherSexMap));
		view.addObject("teacherPosition", JSONObject.toJSONString(teacherPositionMap));
		view.addObject("teacherStatus", JSONObject.toJSONString(teacherStatusMap));
		view.addObject("teacherEducation", JSONObject.toJSONString(teacherEducationMap));
		view.addObject("teacherList", teacherBiz.queryAllTeacherNoDel());
		return view;
	}
	//获得教师列表数据
	@RequestMapping("/teacherListData")
	@ResponseBody
	public String teacherListData(CommonPage commonPage) {
		teacherBiz.queryTeacherByPage(commonPage);
		// System.out.println(commonPage.toSHowPage());
		return commonPage.toSHowPage();
	}
	//获取部门树
	@RequestMapping(value = "/index")
	public ModelAndView deptlist(){
		ModelAndView view = new ModelAndView("ea/teacher/deptIndex");
		Map<Integer, String> teacherSexMap = dictionaryBiz
				.queryDictionaryByParent(DictionaryEnums.SEX.getCode());
		Map<Integer, String> teacherPositionMap = dictionaryBiz
				.queryDictionaryByParent(DictionaryEnums.JOB.getCode());
		Map<Integer, String> teacherStatusMap = dictionaryBiz
				.queryDictionaryByParent(DictionaryEnums.TEACHER_STATUS.getCode());
		Map<Integer, String> teacherEducationMap = dictionaryBiz
				.queryDictionaryByParent(DictionaryEnums.EDUCATION.getCode());
		view.addObject("teacherSexMap", teacherSexMap);
		view.addObject("teacherPositionMap", teacherPositionMap);
		view.addObject("teacherStatusMap", teacherStatusMap);
		view.addObject("teacherEducationMap", teacherEducationMap);
		view.addObject("teacherSex", JSONObject.toJSONString(teacherSexMap));
		view.addObject("teacherPosition", JSONObject.toJSONString(teacherPositionMap));
		view.addObject("teacherStatus", JSONObject.toJSONString(teacherStatusMap));
		view.addObject("teacherEducation", JSONObject.toJSONString(teacherEducationMap));
		view.addObject("teacherList", teacherBiz.queryAllTeacherNoDel());
		return view;
	}
	

	//教师编辑视图
	@RequestMapping("/teacherEdit")
	public ModelAndView teacherEdit(@RequestParam(value = "teacherNo", required = false) Integer teacherNo) {
		ModelAndView view = new ModelAndView("ea/teacher/teacherEdit");
		Map<Integer, String> map = new TreeMap<Integer, String>();
		map=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.EDUCATION.getCode());
		view.addObject("teacherSexMap", dictionaryBiz.queryDictionaryByParent(DictionaryEnums.SEX.getCode()));
		view.addObject("teacherPositionMap", dictionaryBiz.queryDictionaryByParent(DictionaryEnums.JOB.getCode()));
		view.addObject("teacherStatusMap",
				dictionaryBiz.queryDictionaryByParent(DictionaryEnums.TEACHER_STATUS.getCode()));
		view.addObject("teacherEducationMap",map);
		view.addObject("teacherList", teacherBiz.queryAllTeacherNoDel());
		if (teacherNo != null) {
			TeacherBean tea = teacherBiz.queryTeacherByNo(teacherNo);
			String position = tea.getTeacherPosition();
			view.addObject("positions",MultipleTansferUtil.splitStrToList(position));
			view.addObject("teacher", tea);
			
		}
		return view;
	}
	//教师详情视图
	@RequestMapping("/teacherDetail")
	public ModelAndView teacherDetail(@RequestParam(value = "teacherNo", required = false) Integer teacherNo,@RequestParam(value = "isTeacher",required = false) String isTeacher) {
		ModelAndView view = new ModelAndView("ea/teacher/teacherDetail");
		view.addObject("teacherSexMap", dictionaryBiz.queryDictionaryByParent(DictionaryEnums.SEX.getCode()));
		view.addObject("teacherPositionMap", dictionaryBiz.queryDictionaryByParent(DictionaryEnums.JOB.getCode()));
		view.addObject("teacherStatusMap",
				dictionaryBiz.queryDictionaryByParent(DictionaryEnums.TEACHER_STATUS.getCode()));
		view.addObject("teacherEducationMap",
				dictionaryBiz.queryDictionaryByParent(DictionaryEnums.EDUCATION.getCode()));
		List<DictionaryBean> dictionaryList = dictionaryBiz.queryAllDictionary();
		view.addObject("dictionaryList", dictionaryList);
		if (teacherNo != null) {
			List<Integer> ids = new ArrayList<Integer>();
			ids.add(teacherNo);
			List<OaClassRelationBean> oaClassBeans = oaClassRelationBiz.queryOaClassRelationByPersonId(ids);
			if (oaClassBeans != null) {
				List<OaClassBean> oaClass = oaClassBiz.queryOaclass(new OaClassExample());
				view.addObject("teacher", teacherBiz.queryTeacherByNo(teacherNo));
				view.addObject("oaClassRelation", oaClassBeans);
				view.addObject("oaClass", oaClass);
				view.addObject("teacherList", teacherBiz.queryAllTeacherNoDel());
			} else {
				view.addObject("teacher", teacherBiz.queryTeacherByNo(teacherNo));
				view.addObject("teacherList", teacherBiz.queryAllTeacherNoDel());
			}
			if(isTeacher !=null){
				view.addObject("isTeacher", true);
			}else{
				view.addObject("isTeacher", false);
			}
			view.addObject("positions",MultipleTansferUtil.splitStrToList(teacherBiz.queryTeacherByNo(teacherNo).getTeacherPosition()));
		}
		return view;
	}
//保存
	@RequestMapping("/save")
	public @ResponseBody String save(TeacherBean teacherBeans) {
		return JSONObject.toJSONString(teacherBiz.editTeacher(teacherBeans));
	}
//删除
	@RequestMapping("/delete")
	public @ResponseBody String delete(@RequestBody List<Integer> ids) {
		return JSONObject.toJSONString(teacherBiz.deleteTeacher(ids));
	}
	//验证是否存在
	@RequestMapping("/check")
	public @ResponseBody String checkIsExist(@RequestBody TeacherBean teacherBean) {
		return JSONObject.toJSONString(teacherBiz.checkByTeacherBean(teacherBean));
	}
}
